package com.learn.datasource.custom;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;

import java.util.UUID;

/**
 * @create: 2023-04-18 10:00
 * @author: Mr.Du
 * --------------
 * @notes: 自定义并行数据源
 **/
public class CustomSourceWithParallelDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> orderDataStreamSource = env.addSource(new MyParallelSource());
        orderDataStreamSource.print();
        env.execute("");
    }
}

class MyParallelSource implements ParallelSourceFunction<String> {

    @Override
    public void run(SourceContext<String> sctx) throws Exception {
       sctx.collect(UUID.randomUUID().toString());
                   /*
            如果不设置无限循环可以看出，设置了多少并行度就打印出多少条数据
             */
    }

    @Override
    public void cancel() {

    }
}
